package mhnm.projectgoodmorning;

import java.io.IOException;
import mhnm.projectgoodmorning.R;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	protected static final int REQUEST_ENABLE_BT = 1;

	BTController bTController;
	
	private ServiceConnection bTServiceConnection  = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			bTController = ((BTController.LocalBinder)service).getService();
			
			Toast.makeText(MainActivity.this, "Connected to BTService.", Toast.LENGTH_SHORT).show();
			
			registerListener();
			setupConnection();
		}
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			bTController = null;
			
			Toast.makeText(MainActivity.this, "Disconnected from BTService.", Toast.LENGTH_SHORT).show();
		}
	};		 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        startService( new Intent( this, BTController.class ) );
    }
    
    @Override
    protected void onStart() {
        bindService(new Intent(this, BTController.class), bTServiceConnection, Context.BIND_AUTO_CREATE);
        
    	super.onStart();
    }
    
    @Override
    protected void onStop() {
    	unbindService( bTServiceConnection );
    	
    	super.onStop();
    }
    
    @Override
    protected void onDestroy() {

    	super.onDestroy();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	MenuItem icon = (MenuItem) menu.findItem(R.id.menReconnect);
    	icon.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
			
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				bTController.close();
				setupConnection();
				
				return true;
			}
		});
    	
    	return super.onPrepareOptionsMenu(menu);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if( requestCode == REQUEST_ENABLE_BT ) {
    		if( resultCode == RESULT_OK ) {
    			setupConnection();
    		} else if( resultCode == RESULT_CANCELED ) {
    			finish();
    		}
    	}
    	
    	super.onActivityResult(requestCode, resultCode, data);
    }
    
    private void setupConnection() {
    	if( bTController.isConnected() ) {
    		return;
    	}
    	
	    if( bTController.isEnabled() ) {
        	try {
				bTController.connect( "00:12:6F:22:53:65" );
				Toast.makeText(getBaseContext(), "Connected to Device.", Toast.LENGTH_SHORT).show();
			} catch (IOException e) {
				Toast.makeText(getBaseContext(), "Can't connected to Device.", Toast.LENGTH_SHORT).show();
			}
        } else {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }
    
    private void registerListener() {
        Button button = (Button) findViewById(R.id.btnOn);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(), bTController, "on\n") );
        
        button = (Button) findViewById(R.id.btnOff);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "off\n") );
        
        button = (Button) findViewById(R.id.btnIncInt);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd1\n") );
        
        button = (Button) findViewById(R.id.btnDecInt);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd128\n") );
        
        
        button = (Button) findViewById(R.id.btnRed);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd32\n") );
        
        button = (Button) findViewById(R.id.btnGreen);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd160\n") );
        
        button = (Button) findViewById(R.id.btnBlue);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd96\n") );
        
        button = (Button) findViewById(R.id.btnWhite);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd224\n") );
        
        
        button = (Button) findViewById(R.id.btnRed1);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd16\n") );
        
        button = (Button) findViewById(R.id.btnGreen1);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd144\n") );
        
        button = (Button) findViewById(R.id.btnBlue1);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd80\n") );
        
        button = (Button) findViewById(R.id.btnFlash);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd208\n") );
        
        
        button = (Button) findViewById(R.id.btnRed2);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd48\n") );
        
        button = (Button) findViewById(R.id.btnGreen2);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd177\n") );
        
        button = (Button) findViewById(R.id.btnBlue2);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd112\n") );
        
        button = (Button) findViewById(R.id.btnStrobe);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd240\n") );
        
        
        button = (Button) findViewById(R.id.btnRed3);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd8\n") );
        
        button = (Button) findViewById(R.id.btnGreen3);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd136\n") );
        
        button = (Button) findViewById(R.id.btnBlue3);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd72\n") );
        
        button = (Button) findViewById(R.id.btnFade);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd200\n") );
        
        
        button = (Button) findViewById(R.id.btnRed4);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd40\n") );
        
        button = (Button) findViewById(R.id.btnGreen4);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd136\n") );
        
        button = (Button) findViewById(R.id.btnBlue4);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd104\n") );
        
        button = (Button) findViewById(R.id.btnSmooth);
        button.setOnClickListener( new RemoteClickListener(this.getApplicationContext(),  bTController, "cmd232\n") );
    }
}

